home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11637 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  5.3 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: State machine design
  5. Date: 25 Mar 1996 08:21:33 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4j6h6dINNd6o@keats.ugrad.cs.ubc.ca>
  8. References: <4j0ikl$9t5@news.NetVision.net.il>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4j0ikl$9t5@news.NetVision.net.il>,
  12. Uri Simchoni <simchoni@netvision.net.il> wrote:
  13. >Hi,
  14. >
  15. >I'm designing what I call a communication server. This a program which 
  16. >receives messages from the network sends these messages to a device over 
  17. >the serial port according to the device's proprietary protocol, and sends 
  18. >the device's reply back over the network.
  19. >
  20. >What I have in mind is to split the program into three basic modules. The 
  21. >first one handles the RS232 communications, the second handles the 
  22. >protocol logic and the third handles network traffic. Due to operating 
  23. >system constraints (the OS is DOS and the computer the program runs on 
  24. >should do other things), I can't use a model of "wait for a call from the 
  25. >network, process that call and send your reply". I have to use some kind 
  26.  
  27. So don't use DOS! Duh.
  28.  
  29. Why bang your head against a wall when you can load up FreeBSD or Linux?
  30.  
  31. The sort of problem you are talking about has already been solved in these
  32. systems, or is readily implemented.
  33.  
  34. Advantages:
  35.  
  36.     the OS already has efficient, interrupt-driven serial drivers. You just
  37.     learn the POSIX.1 tty API and use that.
  38.  
  39.     there is support for multi-port serial cards, so expanding to handling
  40.     modem banks would be quite easy.
  41.  
  42.     there is networking support for a bunch of different protocols, but
  43.     primarily TCP/IP.
  44.  
  45.     there is pre-emptive multi-tasking.
  46.  
  47.     these things are tested by lots of users who _use_ networking and
  48.     serial stuff day in and day out! Writing your own code would be yet
  49.     another from-scratch deal that has to be heavily tested.
  50.  
  51.     these systems are _free_!
  52.  
  53. What you are demanding could be easily implemented in a fun-filled afternoon
  54. under a UNIX system.
  55.  
  56.  
  57. >of a state machine in the protocol logic module. The protocol logic 
  58. >module will have functions that get called by the other modules whenever 
  59. >an event occurs (clock tick, character arriving from the serial port, 
  60. >request from the network, etc.)
  61.  
  62. In other words, you want to re-invent the wheel. These things are already done
  63. inside the Linux kernel. Multiplexing between characters arriving from the
  64. serial port and messages from the network can be done synchronously using the
  65. select() operating system call, which can simultaneously monitor a number of
  66. file descriptors for input, output or exception conditions, and tell you which
  67. ones are ready for action. It can wait forever, or for the specified timeout
  68. period.
  69.  
  70. If you insist on using a clock interrupt for something, you set a C signal
  71. handler, and start a periodic alarm timer.
  72.  
  73. It's extremely easy once you learn the API. You just open the right files and
  74. sockets and off you go!
  75.  
  76. >After this LONG outline of the problem I get to my question. I'm 
  77. >considering several designs for the state machine interface. One is to 
  78. >have a distinct function for each type of event. The function decides 
  79. >what to do according to module's internal data. Another is to have a 
  80. >pointer to the function that handles all events for the current state. If 
  81. >the state should change, the pointer is modified. Another design lays 
  82. >somewhere in between - to have a sparate function for each type of event 
  83. >and each state.
  84.  
  85. The accepted methodology is to split things into lower-half/upper-half drivers.
  86. For example, for handling the serial input, you buffer the characters into a
  87. queue inside an autonomous lower-half interrupt handler. The rest of the
  88. program makes requests by calling the upper-half code to read the characters
  89. from the queue. The upper and lower halves communicate using shared data
  90. structures, usually semaphores and queues, which allow the consumer of data to
  91. wait when the queue is empty, and the producer to block or do some flow control
  92. operation when the queue is full.
  93.  
  94. You can set up some sort of asynchronous high-level notification of the arrival
  95. of data that roughly corresponds to low-level interrupts, but  in a simple
  96. application like this you might just get away with polling all the various
  97. queues for input and making a decision about what to do based on what messages
  98. have arrived over the network or from the serial protocol.
  99.  
  100. The facilities to do all this are already implemented for you in a decent OS,
  101. you just write the code to initialize and organize the facilities to do the
  102. task.
  103.  
  104. >Since my experience with such program is nearly zero, I cannot tell if 
  105. >one design is preferable over the other (or if there's a better way of 
  106. >doing these things). I'd appreciate anyone's input on the subject. I 
  107. >think the primary issues that should be considered are ease of 
  108. >implementation(handling reentrancy problems, etc), debugging, and 
  109. >readability. Ease of making changes isn't a major issue since the serial 
  110. >protocol is fixed and well established.
  111.  
  112. You might _learn_ some things by doing all this yourself: but a reading decent
  113. textbook on OS design might be helpful, followed up by research into
  114. communication protocols, device drivers and such. But a practical solution
  115. calls for using software that already has these things.
  116. -- 
  117.  
  118.